package com.dys.controller;

import com.dys.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 疾风
 * @ClassName: LoginServlet
 * @Description: 登录的Servlet
 * @CreateDate: 2024/09/25
 * @Version: v1.0.0
 */
@WebServlet(name = "loginServlet", urlPatterns = "/loginServlet.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行LoginServlet类中的service()方法......");
        // 设置post提交的字符集
        // req.setCharacterEncoding("utf-8");
        // 获取请求参数
        String username = req.getParameter("user");
        System.out.println("账号：" + username);
        String password = req.getParameter("pwd");
        System.out.println("密码：" + password);
        // 连接数据库,校验账号和密码
        // 登录成功，将用户信息放入Session域
        User user = new User(username, password);
        req.getSession().setAttribute("user",user);
        // 跳转到主页(1.请求转发 2.响应重定向)
        // req.getRequestDispatcher("main.jsp").forward(req,resp);
        resp.sendRedirect("main.jsp");
        // resp.getWriter().println("登录成功!");
    }
}
